package com.learner.common.exception;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
* @author ice2020x
* @Date: 2023/3/10
* @Description: 
**/
@Slf4j
public class CommonExceptionUtil {


    /**
     * 异常处理转换
     *
     * @param exception 待处理异常
     * @param convertMsg 转换信息
     * @return
     */
    public static RestException process(Exception exception, String convertMsg) {
        RestException restException = new RestException(convertMsg, exception);
        return process(restException);
    }


    /**
     * 全局处理转换
     *
     * @param exception 待处理异常
     * @return
     */
    public static RestException process(Exception exception) {
        String message = exception.getMessage();
        log.error(String.format("发生异常：%s，异常信息为：%s", exception.getClass().getSimpleName(), message), exception);
        if (exception instanceof RestException && StringUtils.isNotEmpty(message)) {
            return (RestException) exception;
        }
        return new RestException(-1, "系统繁忙，请稍后再试");
    }
}
